



<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      
        <meta name="description" content="Distributed Computing for AI Made Simple">
      
      
      
        <meta name="author" content="Jiale Zhi">
      
      
        <meta name="lang:clipboard.copy" content="Copy to clipboard">
      
        <meta name="lang:clipboard.copied" content="Copied to clipboard">
      
        <meta name="lang:search.language" content="en">
      
        <meta name="lang:search.pipeline.stopwords" content="True">
      
        <meta name="lang:search.pipeline.trimmer" content="True">
      
        <meta name="lang:search.result.none" content="No matching documents">
      
        <meta name="lang:search.result.one" content="1 matching document">
      
        <meta name="lang:search.result.other" content="# matching documents">
      
        <meta name="lang:search.tokenizer" content="[\s\-]+">
      
      <link rel="shortcut icon" href="../img/favicon.ico">
      <meta name="generator" content="mkdocs-1.0.4, mkdocs-material-4.6.0">
    
    
      
        <title>Examples - Fiber</title>
      
    
    
      <link rel="stylesheet" href="../assets/stylesheets/application.1b62728e.css">
      
        <link rel="stylesheet" href="../assets/stylesheets/application-palette.a8b3c06d.css">
      
      
        
        
        <meta name="theme-color" content="">
      
    
    
      <script src="../assets/javascripts/modernizr.268332fc.js"></script>
    
    
      
        <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
        <style>body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}</style>
      
    
    <link rel="stylesheet" href="../assets/fonts/material-icons.css">
    
    
    
      
    
    
  </head>
  
    
    
    <body dir="ltr" data-md-color-primary="white" data-md-color-accent="">
  
    <svg class="md-svg">
      <defs>
        
        
          <svg xmlns="http://www.w3.org/2000/svg" width="416" height="448" viewBox="0 0 416 448" id="__github"><path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z"/></svg>
        
      </defs>
    </svg>
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
    
      <a href="#run-openai-baselines-on-kubernetes-with-fiber" tabindex="1" class="md-skip">
        Skip to content
      </a>
    
    
      <header class="md-header" data-md-component="header">

    <nav class="md-header-nav md-grid">
        <div class="md-flex">
            <div class="md-flex__cell md-flex__cell--shrink">
                <a class="md-header-nav__button md-logo"
                   href=".." title="Fiber">
                    <img src="../img/fiber_logo.png" style="height:1.4rem;">
                </a>
            </div>
            <div class="md-flex__cell md-flex__cell--shrink">
                <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
            </div>
            <div class="md-flex__cell md-flex__cell--stretch">
                <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
                    
                    <span class="md-header-nav__topic">
                    Examples
                    </span>
                    
                </div>
            </div>
            <div class="md-flex__cell md-flex__cell--shrink">
                
                
                <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
                
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
                
                
            </div>
            
            <div class="md-flex__cell md-flex__cell--shrink">
                <div class="md-header-nav__source">
                    


  

<a href="https://github.com/uber/fiber/" title="Go to repository" class="md-source" data-md-source="github">
  
    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>
  
  <div class="md-source__repository">
    uber/fiber
  </div>
</a>
                </div>
            </div>
            
        </div>
    </nav>
</header>
    
    <div class="md-container">
      
        
      
      
      <main class="md-main" role="main">
        <div class="md-main__inner md-grid" data-md-component="container">
          
            
              <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    <nav class="md-nav md-nav--primary" data-md-level="0">
    <label class="md-nav__title md-nav__title--site" for="__drawer">
        <a class="md-nav__button md-logo" href=".."
           title="Fiber">
            <img src="../img/fiber_logo.png" style="width: 10rem">
        </a>
    </label>
    
    <div class="md-nav__source">
        


  

<a href="https://github.com/uber/fiber/" title="Go to repository" class="md-source" data-md-source="github">
  
    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>
  
  <div class="md-source__repository">
    uber/fiber
  </div>
</a>
    </div>
    
    <ul class="md-nav__list" data-md-scrollfix>
        
        
        
        


  <li class="md-nav__item">
    <a href=".." title="Home" class="md-nav__link">
      Home
    </a>
  </li>

        
        
        
        


  <li class="md-nav__item">
    <a href="../why/" title="Why Use Fiber" class="md-nav__link">
      Why Use Fiber
    </a>
  </li>

        
        
        
        


  <li class="md-nav__item">
    <a href="../introduction/" title="Introduction to Fiber" class="md-nav__link">
      Introduction to Fiber
    </a>
  </li>

        
        
        
        


  <li class="md-nav__item">
    <a href="../getting-started/" title="Getting Started" class="md-nav__link">
      Getting Started
    </a>
  </li>

        
        
        
        


  <li class="md-nav__item">
    <a href="../advanced/" title="More about Fiber" class="md-nav__link">
      More about Fiber
    </a>
  </li>

        
        
        
        

  


  <li class="md-nav__item md-nav__item--active">
    
    <input class="md-toggle md-nav__toggle" data-md-toggle="toc" type="checkbox" id="__toc">
    
    
      <label class="md-nav__link md-nav__link--active" for="__toc">
        Examples
      </label>
    
    <a href="./" title="Examples" class="md-nav__link md-nav__link--active">
      Examples
    </a>
    
      
<nav class="md-nav md-nav--secondary">
  
  
  
    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#run-openai-baselines-on-kubernetes-with-fiber" class="md-nav__link">
    Run OpenAI Baselines on Kubernetes with Fiber
  </a>
  
    <nav class="md-nav">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#prepare-the-code" class="md-nav__link">
    Prepare the code
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#fiberization-or-adapt-your-code-to-run-with-fiber" class="md-nav__link">
    Fiberization (Or adapt your code to run with Fiber)
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#containerize-the-application" class="md-nav__link">
    Containerize the application
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#running-on-kubernetes" class="md-nav__link">
    Running on Kubernetes
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
      
      
      
      
    </ul>
  
</nav>
    
  </li>

        
        
        
        


  <li class="md-nav__item">
    <a href="../installation/" title="Installation" class="md-nav__link">
      Installation
    </a>
  </li>

        
        
        
        


  <li class="md-nav__item">
    <a href="../platforms/" title="Platforms and Backends" class="md-nav__link">
      Platforms and Backends
    </a>
  </li>

        
        
        
        


  <li class="md-nav__item md-nav__item--nested">
    
      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-9" type="checkbox" id="nav-9">
    
    <label class="md-nav__link" for="nav-9">
      API Documentation
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-9">
        API Documentation
      </label>
      <ul class="md-nav__list" data-md-scrollfix>
        
        
          
          
          


  <li class="md-nav__item">
    <a href="../process/" title="Process" class="md-nav__link">
      Process
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../queues/" title="Queues" class="md-nav__link">
      Queues
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../pool/" title="Pool" class="md-nav__link">
      Pool
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../managers/" title="Managers" class="md-nav__link">
      Managers
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../experimental/ring/" title="Ring" class="md-nav__link">
      Ring
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../config/" title="Config" class="md-nav__link">
      Config
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../meta/" title="Metadata" class="md-nav__link">
      Metadata
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../cli/" title="Command Line Tool" class="md-nav__link">
      Command Line Tool
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../misc/" title="Misc" class="md-nav__link">
      Misc
    </a>
  </li>

        
      </ul>
    </nav>
  </li>

        
    </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    
<nav class="md-nav md-nav--secondary">
  
  
  
    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#run-openai-baselines-on-kubernetes-with-fiber" class="md-nav__link">
    Run OpenAI Baselines on Kubernetes with Fiber
  </a>
  
    <nav class="md-nav">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#prepare-the-code" class="md-nav__link">
    Prepare the code
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#fiberization-or-adapt-your-code-to-run-with-fiber" class="md-nav__link">
    Fiberization (Or adapt your code to run with Fiber)
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#containerize-the-application" class="md-nav__link">
    Containerize the application
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#running-on-kubernetes" class="md-nav__link">
    Running on Kubernetes
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
      
      
      
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content">
            <article class="md-content__inner md-typeset">
              
                
                  <a href="https://github.com/uber/fiber/edit/master/mkdocs/examples.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>
                
                
                  <h1>Examples</h1>
                
                <h3 id="run-openai-baselines-on-kubernetes-with-fiber">Run OpenAI Baselines on Kubernetes with Fiber<a class="headerlink" href="#run-openai-baselines-on-kubernetes-with-fiber" title="Permanent link">&para;</a></h3>
<p>In this example, we'll show you how to integrate fiber with OpenAI baselines with just <strong>one line</strong> of code change.</p>
<p>If your project is already using Python's multiprocessing, then integrate it with Fiber is very easy. Here, we are going to use <a href="https://github.com/openai/baselines">OpenAI Baselines</a> as an example to show how to easily run code written with multiprocessing on Kubernetes easily.</p>
<h4 id="prepare-the-code">Prepare the code<a class="headerlink" href="#prepare-the-code" title="Permanent link">&para;</a></h4>
<p>First, we clone baselines from Github, create a new branch and setup our local environment:</p>
<div class="codehilite"><pre><span></span>git clone https://github.com/openai/baselines
<span class="nb">cd</span> baselines
git checkout -b fiber
virtualenv -p python3 env
. env/bin/activate
<span class="nb">echo</span> <span class="s2">&quot;env&quot;</span> &gt; .dockerignore
pip install <span class="s2">&quot;tensorflow&lt;2&quot;</span>
pip install -e .
pip install fiber
</pre></div>


<p>Test that if the environment works:</p>
<div class="codehilite"><pre><span></span>python -m baselines.run --alg=ppo2 --env=CartPole-v0 --network=mlp --num_timesteps=10000
</pre></div>


<p>If it works, you should see something like this in your output</p>
<div class="codehilite"><pre><span></span>---------------------------------------
| eplenmean               | 23.8      |
| eprewmean               | 23.8      |
| fps                     | 1.95e+03  |
| loss/approxkl           | 0.000232  |
| loss/clipfrac           | 0         |
| loss/policy_entropy     | 0.693     |
| loss/policy_loss        | -0.00224  |
| loss/value_loss         | 48.4      |
| misc/explained_variance | -0.000784 |
| misc/nupdates           | 1         |
| misc/serial_timesteps   | 2.05e+03  |
| misc/time_elapsed       | 1.05      |
| misc/total_timesteps    | 2.05e+03  |
---------------------------------------
</pre></div>


<p>OpenAI baselines has a <code>SubprocVecEnv</code> that, according to it's documentation, runs multiple environments in parallel in subprocesses and communicates with them via pipes. We'll start from here to modify it to work with Fiber:</p>
<h4 id="fiberization-or-adapt-your-code-to-run-with-fiber">Fiberization (Or adapt your code to run with Fiber)<a class="headerlink" href="#fiberization-or-adapt-your-code-to-run-with-fiber" title="Permanent link">&para;</a></h4>
<p>Open <code>baselines/common/vec_env/subproc_vec_env.py</code> and change this line:</p>
<div class="codehilite"><pre><span></span><span class="kn">import</span> <span class="nn">multiprocessing</span> <span class="k">as</span> <span class="nn">mp</span>
</pre></div>


<p>to</p>
<div class="codehilite"><pre><span></span><span class="kn">import</span> <span class="nn">fiber</span> <span class="k">as</span> <span class="nn">mp</span>
</pre></div>


<p>Let's do a quick test to see if this change works</p>
<div class="codehilite"><pre><span></span>python -m baselines.run --alg<span class="o">=</span>ppo2 --env<span class="o">=</span>CartPole-v0 --network<span class="o">=</span>mlp --num_timesteps<span class="o">=</span><span class="m">10000</span> --num_env <span class="m">2</span>
</pre></div>


<p>We set <code>--num_env 2</code> to make sure baselines is using <code>SubprocVecEnv</code>. If everything works, we should see similar output as the previous run.</p>
<h4 id="containerize-the-application">Containerize the application<a class="headerlink" href="#containerize-the-application" title="Permanent link">&para;</a></h4>
<p>OpenAI baselines already has a Dockerfile available, so we just need to add fiber to it by adding a line <code>RUN pip install fiber</code>. After modification, the Dockerfile looks like this:</p>
<div class="codehilite"><pre><span></span><span class="k">FROM</span> <span class="s">python:3.6</span>

<span class="k">RUN</span> apt-get -y update <span class="o">&amp;&amp;</span> apt-get -y install ffmpeg
<span class="c"># RUN apt-get -y update &amp;&amp; apt-get -y install git wget python-dev python3-dev libopenmpi-dev python-pip zlib1g-dev cmake python-opencv</span>

<span class="k">ENV</span> CODE_DIR /root/code

<span class="k">COPY</span> . <span class="nv">$CODE_DIR</span>/baselines
<span class="k">WORKDIR</span><span class="s"> $CODE_DIR/baselines</span>

<span class="c"># Clean up pycache and pyc files</span>
<span class="k">RUN</span> rm -rf __pycache__ <span class="o">&amp;&amp;</span> <span class="se">\</span>
    find . -name <span class="s2">&quot;*.pyc&quot;</span> -delete <span class="o">&amp;&amp;</span> <span class="se">\</span>
    pip install <span class="s1">&#39;tensorflow &lt; 2&#39;</span> <span class="o">&amp;&amp;</span> <span class="se">\</span>
    pip install -e .<span class="o">[</span>test<span class="o">]</span>

<span class="k">RUN</span> pip install fiber

<span class="k">CMD</span> /bin/bash
</pre></div>


<p>It's a good habit to make sure everything works locally before submitting the job to the bigger cluster because this will save you a lot of debugging time. So we build our docker image locally:</p>
<div class="codehilite"><pre><span></span>docker build -t fiber-openai-baselines .
</pre></div>


<p>When Fiber starts new dockers locally, it will mount your home directory into docker. So we need to modify baselines' log dir to make sure it can write logs to the correct place by adding an argument <code>--log_path=logs</code>. By default, baselines writes to <code>/tmp</code> dir which is not shared by Fiber master process and subprocesses. We also add <code>--num_env 2</code> to make sure baselines uses <code>SubprocVecEnv</code> so that Fiber processes can be launched.</p>
<div class="codehilite"><pre><span></span><span class="nv">FIBER_BACKEND</span><span class="o">=</span>docker <span class="nv">FIBER_IMAGE</span><span class="o">=</span>fiber-openai-baselines:latest python -m baselines.run --alg<span class="o">=</span>ppo2 --env<span class="o">=</span>CartPole-v0 --network<span class="o">=</span>mlp --num_timesteps<span class="o">=</span><span class="m">10000</span> --num_env <span class="m">2</span> --log_path<span class="o">=</span>logs
</pre></div>


<h4 id="running-on-kubernetes">Running on Kubernetes<a class="headerlink" href="#running-on-kubernetes" title="Permanent link">&para;</a></h4>
<p>Now let's run our fiberized OpenAI baselines on Kubernetes. This time we run <code>1e7</code> time steps. Also, we want to store the output of the run on persistent storage. We can do this with <code>fiber</code> command's <a href="../advanced/#working-with-persistent-storage">mounting persistent volumes</a> feature.</p>
<div class="codehilite"><pre><span></span>$ fiber run -v fiber-pv-claim python -m baselines.run --alg<span class="o">=</span>ppo2 --env<span class="o">=</span>CartPole-v0 --network<span class="o">=</span>mlp --num_timesteps<span class="o">=</span>1e7 --num_env <span class="m">2</span> --log_path<span class="o">=</span>/persistent/baselines/logs/
</pre></div>


<p>It should output something like this:</p>
<div class="codehilite"><pre><span></span>Created pod: baselines-d00eb2ef
</pre></div>


<p>After the job is done, you can copy the logs with these commands:</p>
<div class="codehilite"><pre><span></span>$ fiber cp fiber-pv-claim:/persistent/baselines/logs baselines-logs
</pre></div>
                
                  
                
                
              
              
                


              
            </article>
          </div>
        </div>
      </main>
      
        

<!-- Application footer -->
<footer class="md-footer">

    <!-- Link to previous and/or next page -->
    
    <div class="md-footer-nav">
        <nav class="md-footer-nav__inner md-grid">

            <!-- Link to previous page -->
            
            <a class="md-flex md-footer-nav__link md-footer-nav__link--prev"
               href="../advanced/"
               rel="prev"
               title="More about Fiber">
                <div class="md-flex__cell md-flex__cell--shrink">
                    <i class="md-icon md-icon--arrow-back
                    md-footer-nav__button"></i>
                </div>
                <div class="md-flex__cell md-flex__cell--stretch
                  md-footer-nav__title">
              <span class="md-flex__ellipsis">
                <span class="md-footer-nav__direction">
                  Previous
                </span>
                More about Fiber
              </span>
                </div>
            </a>
            

            <!-- Link to next page -->
            
            <a class="md-flex md-footer-nav__link md-footer-nav__link--next"
               href="../installation/"
               rel="next"
               title="Installation">
                <div class="md-flex__cell md-flex__cell--stretch
                  md-footer-nav__title">
              <span class="md-flex__ellipsis">
                <span class="md-footer-nav__direction">
                  Next
                </span>
                Installation
              </span>
                </div>
                <div class="md-flex__cell md-flex__cell--shrink">
                    <i class="md-icon md-icon--arrow-forward
                    md-footer-nav__button"></i>
                </div>
            </a>
            
        </nav>
    </div>
    

    <!-- Further information -->
    <div class="md-footer-meta md-typeset">
        <div class="md-footer-meta__inner md-grid">

            <!-- Copyright and theme information -->
            <div class="md-footer-copyright">
                <div class="footer-logo-smallpad"></div>
                
                <div class="md-footer-copyright__highlight">
                    Copyright &copy; 2020 Uber Technologies Inc.
                </div>
                
                Website by <a href="https://twitter.com/_calio">calio</a> powered by
                <a href="https://www.mkdocs.org">MkDocs</a>,
                <a href="https://squidfunk.github.io/mkdocs-material/">Material for MkDocs</a>,
                <a href="http://www.styleshout.com/">styleshout</a>.
            </div>

            <!-- Social links -->
            
            
            
        </div>
    </div>
</footer>
      
    </div>
    
      <script src="../assets/javascripts/application.808e90bb.js"></script>
      
      <script>app.initialize({version:"1.0.4",url:{base:".."}})</script>
      
    
  </body>
</html>